<template>
    <div class="jenkins-home-index">
        <div>{{secs ? secs+'秒后在新标签页中打开Jenkins。' : '已经在新标签页中打开Jenkins'}}注意：需使用最新版浏览器（例如firefox的最新版本）</div>
        <br>
        <div>点此<a :href="url">go</a>在本页中打开Jenkins（需在10秒内操作，否则自动登录将过期）</div>
        
    </div>
</template>
<script>
    import {mapState} from 'vuex'
    import {selfList as getProjectList, list, list as listProject} from "../../api/project";
    import { gotoJenkins } from "../../api/jenkins";
    
    export default {
        components: {
        },
        data() {
            return {
                url:'url',
                secs:10,
            }
        },
        computed: {
            ...mapState({
                userInfo: state => state.userInfo,
                socketAction: state => state.socketAction,
            }),
        },
        created() {
            this.init(true, true, true);
        },
        watch:{
            $route: function (to, from) {
                this.init();
            },
        },
        methods: {
            init(reset = true, loading = true) {
                this.gotoJenkins(loading);
            },
            gotoJenkins(loading, all) {
                console.log('gotoJenkins 1')
                all = 2
                let param = {...this.requestData, all: all, pageSize:1000}
                // if(this.project.all) {
                //     delete param.memberCode
                // } else {
                //     if(this.$route.query.memberCode) {
                //         param.memberCode = this.$route.query.memberCode
                //     } else {
                //         param.memberCode = this.userInfo.code
                //     }
                // }

                let app = this
                console.log('gotoJenkins 2', gotoJenkins)
                return gotoJenkins(null, null, param).then(res => {
                    console.log('gotoJenkins 3')
                    console.log('res', res)
                    if(res.data) {
                        res = res.data
                    }
                    if(res.loginMember && res.loginUrl && res.loginMember.email) {
                        let self = this
                        let url = res.loginUrl+'&auto=true;name='+res.loginMember.email.split('@')[0]
                                +';mobile='+res.loginMember.mobile
                        console.log('url', self.url, url)
                        self.url = url
                        console.log('url', url)

                        if(window.location.href.indexOf('to=jenkins') >= 0) {
                            window.location.replace(url)
                        } else {
                            window.open(url, '_blank')
                        }
                        app.$router.replace('/');
                        // this.secs = 5
                        // let cb = () => {
                        //     this.secs--
                        //     if(this.secs == 0) {
                        //         try {
                        //         window.open(url, '_blank')
                        //         }catch(ex) {
                        //             console.error('ex', ex)
                        //         }
                        //     } else {
                        //         setTimeout(cb, 1000)
                        //     }
                        // }
                        // setTimeout(cb, 10)
                    } else {
                        this.$message.error('返回结果非法')
                    }
                    
                });
            },
        }
    }
</script>
<style lang="less">
    .gitTooltipClass {
        width:40vw;
        .ant-popover-content {
            .ant-popover-inner {
                background-color: peachpuff;
            }
        }
    }

    .jenkins-home-index {
        width:100%;
        height: 100;
        font-size: 24px;
        margin-top: 2em;
        text-align: center;
        margin-bottom: 3em;
    }
</style>
